import CryptoJS from 'crypto-js'

// 本地存储
export function setItem(key, val) {
    if (val === '' || val === undefined || val === null) return
    if (typeof val !== 'string') val = JSON.stringify(val)
    try {
        window.localStorage.setItem(key, val)
    } catch (e) {
        // removeItem('userAction');
    }
}

/**
 * AES加密
 */
export function encrypt(code) {
    let key = CryptoJS.enc.Utf8.parse('I6bPqY4nJqZ1Nk3S')
    let srcs = CryptoJS.enc.Utf8.parse(code)
    let encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
    return encrypted.toString()
}

/**
 * AES解密
 */
export function decrypt(code) {
    let key = CryptoJS.enc.Utf8.parse('I6bPqY4nJqZ1Nk3S')
    let decrypt = CryptoJS.AES.decrypt(code, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
    return CryptoJS.enc.Utf8.stringify(decrypt).toString()
}

// 是否嵌入在Deepcoin app中
export function inDeepcoinApp() {
    return window.navigator.userAgent.includes('3RAPP')
}

// 读取本地存储
export function getItem(key, defaultVal) {
    if (!key) return defaultVal
    if (typeof key !== 'string') key = JSON.stringify(key)
    // 特殊处理 localUserList
    if (key === 'localUserList') {
        let _localUserList = window.localStorage.getItem('localUserList')
        if (!_localUserList) return encrypt('[]')
        if (_localUserList.includes('token')) {
            // 若未加密，则先加密存储后再返回
            let result = encrypt(_localUserList)
            window.localStorage.setItem('localUserList', result)
            return result
        }
    }
    // 特殊处理 userInfo
    if (key === 'userInfo') {
        let _userInfo = window.localStorage.getItem('userInfo')
        if (!_userInfo) return ''
        if (_userInfo.includes('token')) {
            // 若未加密，则先加密存储后再返回
            let result2 = encrypt(_userInfo)
            window.localStorage.setItem('userInfo', result2)
            return result2
        }
    }
    return window.localStorage.getItem(key) || defaultVal
}

// 删除本地存储
export function removeItem(key) {
    if (!key) return
    if (typeof key !== 'string') key = JSON.stringify(key)
    try {
        window.localStorage.removeItem(key)
    } catch (e) {}
}

// 本地存储
export function setItem2(key, val) {
    if (val === '' || val === undefined || val === null) return
    if (typeof val !== 'string') val = JSON.stringify(val)
    try {
        window.sessionStorage.setItem(key, val)
    } catch (e) {
        // removeItem2('userAction');
    }
}

// 读取本地存储
export function getItem2(key, defaultVal) {
    if (!key) return defaultVal
    if (typeof key !== 'string') key = JSON.stringify(key)

    const val = window.sessionStorage.getItem(key) || defaultVal
    if (val && defaultVal instanceof Object) {
        try {
            return JSON.parse(val)
        } catch (error) {
            return defaultVal
        }
    } else {
        return val
    }
}

// 删除本地存储
export function removeItem2(key) {
    if (!key) return
    if (typeof key !== 'string') key = JSON.stringify(key)
    try {
        window.sessionStorage.removeItem(key)
    } catch (e) {}
}

/**
 * 从URL中读取语言配置
 */
export function getLangByUrl() {
    let language = window.navigator && window.navigator.language ? window.navigator.language : ''
    let locale = getItem('locale')
    const lang = locale
        ? locale
        : language && language.indexOf('zh') > -1
        ? window.location.origin.endsWith('com')
            ? 'en'
            : 'zh-CN'
        : language && language.indexOf('ja') > -1
        ? 'ja'
        : 'en'
    let _pathName = window.location.pathname
    if (!_pathName || _pathName.length === 1) return lang
    return _pathName.split('/')[1]
}

//获取随机字符串 e 字符串长度
export function randomString(e) {
    e = e || 32
    var t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678',
        a = t.length,
        n = ''
    for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a))
    return n
}